package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.adapters.BuyerServiceListAdapter;
import com.sxm.yiwei.beans.ServiceList;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class BuyerNoDiscusslistActivity extends BaseActivity implements OnClickListener{

    private ImageView nodiscussback;
    private ListView nodiscusslist;
    private SharedPreferences sharedPreferences;
    private String token;
    private String userId;
    private String appId;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buyer_no_discusslist);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        token = sharedPreferences.getString("token", "");
        userId = sharedPreferences.getString("userId", "");
        appId = sharedPreferences.getString("appid", "");
        initNoDiscussView();
        getBuyerOverServiceList();
    }
    // 已完结
    private void getBuyerOverServiceList() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.GETBUYERSERVICELIST);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token", token);
        params.addHeader("appId", appId);
        params.addHeader("userId", userId);
        JSONObject object = new JSONObject();
        object.put("status","4");
        params.addParameter("params",object.toJSONString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                JSONObject obj = JSON.parseObject(result);
                JSONArray data = obj.getJSONArray("data");
                List<ServiceList.DataBean> mlist = new ArrayList<>();
                for (int i = 0; i < data.size(); i++) {
                    JSONObject obj1 = JSON.parseObject(String.valueOf(data.get(i)));
                    String amount = obj1.getString("amount");
                    String avatar = obj1.getString("avatar");
                    String buyTime = obj1.getString("buyTime");
                    String count = obj1.getString("count");
                    String id = obj1.getString("id");
                    String makeupCount = obj1.getString("makeupCount");
                    String message = obj1.getString("message");
                    String name = obj1.getString("name");
                    String orderCode = obj1.getString("orderCode");
                    String serviceId = obj1.getString("serviceId");
                    String totalAmount = obj1.getString("totalAmount");

                    ServiceList.DataBean dataBean = new ServiceList.DataBean();
                    dataBean.setAmount(Double.valueOf(amount));
                    dataBean.setAvatar(avatar);
                    dataBean.setBuyTime(Long.valueOf(buyTime));
                    dataBean.setCount(Integer.valueOf(count));
                    dataBean.setId(id);
                    dataBean.setMakeupCount(Integer.valueOf(makeupCount));
                    dataBean.setMessage(message);
                    dataBean.setName(name);
                    dataBean.setOrderCode(orderCode);
                    dataBean.setServiceId(serviceId);
                    dataBean.setTotalAmount(Double.valueOf(totalAmount));
                    mlist.add(dataBean);
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(BuyerNoDiscusslistActivity.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initNoDiscussView() {
        nodiscussback = (ImageView) findViewById(R.id.nodiscuss_back);
        nodiscusslist = (ListView) findViewById(R.id.nodiscusslist);
        backmain = (ImageView) findViewById(R.id.buyernodis_backmain);
        backmain.setOnClickListener(this);
        nodiscussback.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.nodiscuss_back:
                finish();
                break;
            case R.id.buyernodis_backmain:
                startActivity(new Intent(BuyerNoDiscusslistActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
        }
    }
}
